using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public enum BirdState
{
    Waiting,
    BeforeShoot,
    AfterShoot,
    WaitToDie
}
public class Bird : MonoBehaviour
{

    // 等待 发射前 发射后


    // Start is called before the first frame update

    public BirdState state = BirdState.BeforeShoot;
    private bool isMouseDown = false;
    public float maxDistance = 2.45f;
    public float flySpeed = 15;
    protected Rigidbody2D rgd;
    public bool isFlying = true;
    // 是否使用过技能
    public bool isHaveUsedSkill = false;


    void Start()
    {
        rgd = GetComponent<Rigidbody2D>();
        rgd.bodyType = RigidbodyType2D.Static;
    }

    // Update is called once per frame
    void Update()
    {
        switch (state)
        {
            case BirdState.Waiting:
                break;
            case BirdState.BeforeShoot:
                MoveControl();
                break;
            case BirdState.AfterShoot:
                StopControl();
                SkillControl();
                break;
            case BirdState.WaitToDie:
                break;
            default:
                break;
        }
    }

    // 去除等待区碰撞
    

    // OnMouseDown OnMouseUp
    private void OnMouseDown()
    {
        if (state == BirdState.BeforeShoot )
        {
            isMouseDown = true;
            Slingshot.Instance.StratDraw(transform);
            // 鼠标按下发出音效
            AudioManager.Instance.PlayBirdSelect(transform.position);
        }
    }
    private void OnMouseUp()
    {
        if (state == BirdState.BeforeShoot )
        {
            isMouseDown = false;
            Slingshot.Instance.EndDraw();
            Fly();

            
        }
    }

    private void MoveControl()
    {
        if (isMouseDown)
        {
            transform.position = GetMousePositon();
        }
    }

    // 得到鼠标世界的位置
    private Vector3 GetMousePositon()
    {
        Vector3 centerPosition = Slingshot.Instance.getCenterPosition();
        Vector3 mp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        mp.z = 0;
        Vector3 mouseDir = mp - centerPosition;

        // 鼠标点和小鸟的距离
        float distance = mouseDir.magnitude;

        if (distance > maxDistance)
        {
            mp = mouseDir.normalized * maxDistance + centerPosition;
        }

        return mp;
    }

    // 小鸟飞行
    private void Fly()
    {
        rgd.bodyType = RigidbodyType2D.Dynamic;

        rgd.velocity = (Slingshot.Instance.getCenterPosition() - transform.position).normalized * flySpeed;
        state = BirdState.AfterShoot;
        AudioManager.Instance.PlayBirdFlying(transform.position);
    }

    // 提供更改状态方法
    public void GoStage(Vector3 position)
    {
        state = BirdState.BeforeShoot;
        transform.position = position;
    }

    // 判断是否停止
    private void StopControl()
    {
        // 判断速度小于0.1
        if (rgd.velocity.magnitude < 0.1f)
        {
            state = BirdState.WaitToDie;
            Invoke("LoadNextBird", 1f);

        }
    }

    // 技能使用
    private void SkillControl()
    {
        if (isHaveUsedSkill) return;
        if (isFlying && Input.GetMouseButtonDown(0))
        {
            isHaveUsedSkill = true;
            FlyingSkill();
        }
        if (Input.GetMouseButtonDown(0))
        {
            isHaveUsedSkill = true;
            FullTimeSkill();
        }
        
    }

    // 飞翔的方法
    protected virtual void FlyingSkill()
    {
        
    }

    // 全局使用方法
    protected virtual void FullTimeSkill()
    {
        
    }

    // 加载下一个小鸟
    protected void LoadNextBird()
    {
        Destroy(gameObject);
        // 实例化爆炸特效
        GameObject.Instantiate(Resources.Load("Boom1"), transform.position, Quaternion.identity);
        GameManager.Instance.LoadNextBird();
    }

    // 小鸟碰撞
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (state == BirdState.AfterShoot)
        {
            isFlying = false;
        }
        
        // 判断碰撞大小，相对速度 > 3
        if (state == BirdState.AfterShoot && collision.relativeVelocity.magnitude > 5)
        {
            AudioManager.Instance.PlayBirdCollision(transform.position);
        }
    }

    
}
